package tgc.edu.exam.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;

import tgc.edu.exam.custom.CommonService;
import tgc.edu.exam.entity.Books;
import tgc.edu.exam.repository.BookRepository;

@Service
public class BookService extends CommonService<Books, Integer>{

	@Autowired
	private BookRepository dao;
	private String path="c:/files/qmkh";
	
	@Transactional
	public void savebook(MultipartFile file,Books book) {
		
		String filename = file.getOriginalFilename();
		String uuid = UUID.randomUUID().toString();
		book.setFilename(filename);
		book.setUuid(uuid);
		dao.save(book);
		File path = new File(this.path);
		if(!path.isDirectory()) {
			path.mkdir();
		}
		File saveFile = new File(path, uuid);
		try {
			file.transferTo(saveFile);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		
	}

	
	//下载
		public void download(Integer id, HttpServletRequest request, HttpServletResponse response) {
			Books book = findById(id);
			String filename = book.getFilename();
			filename=getStr(request,filename);
			String uuid = book.getUuid();
			File file = new File(path, uuid);
			if(file.exists()) {
				FileInputStream fis;
				try {
					fis = new FileInputStream(file);
					response.setContentType("application/x-msdownload");
					response.addHeader("Content-Disposition", "attachment; filename=" + filename);
					ServletOutputStream out = response.getOutputStream();
					byte[] buf=new byte[2048];
					int n=0;
					while((n=fis.read(buf))!=-1){
						out.write(buf, 0, n);
					}
					fis.close();
					out.flush();
					out.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}catch (IOException e) {
					e.printStackTrace();
				}
			
			}
		}

		private String getStr(HttpServletRequest request, String filename) {
			String downloadFileName = null;
			String agent = request.getHeader("USER-AGENT");
				 try {
					 if(agent != null && agent.toLowerCase().indexOf("firefox")>0) {
					downloadFileName = "=?UTF-8?B?" + Base64Utils.encodeToString(filename.getBytes("UTF-8")) + "?=";
					 }else {
						 downloadFileName =  java.net.URLEncoder.encode(filename, "UTF-8");
					 }
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			return downloadFileName;
		}
		public List<Books> findByTitleLike(String title) {
			
			return dao.findByTitleLike(title);
		}


		public List<Books> findByTypeId(Integer id) {
			
			return dao.findByTypeId(id);
		}


		public Books findByByid(String byid) {
			return dao.findByByid(byid);
		}


		public List<Books> findAllBook() {
			return dao.findAllBook();
		}

}
